﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtlity;
using Models;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class BugService
    {
        #region 新增
        /// <summary>
        /// 新增一条bug记录
        /// </summary>
        /// <param name="bug"></param>
        /// <returns></returns>
        public int InsertBug(BugClass bug)
        {
            string sql = "insert into Bugs(WorkParentId,PName,MName,QuestionType,";
            sql += "QuestionDescription,CompleteDay,MoldPerson,PlanDescription)";
            sql += "values(@WorkParentId,@PName,@MName,@QuestionType,@QuestionDescription,";
            sql += "@CompleteDay,@MoldPerson,@PlanDescription)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",bug.WorkParentId),
                new SqlParameter("@PName",bug.PName),
                new SqlParameter("@MName",bug.MName),
                new SqlParameter("@QuestionType",bug.QuestionType),
                new SqlParameter("@QuestionDescription",bug.QuestionDescription),
                new SqlParameter("@CompleteDay",bug.CompleteDay),
                new SqlParameter("@MoldPerson",bug.MoldPerson),
                new SqlParameter("@PlanDescription",bug.PlanDescription)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据WorkParentId查询Bug表中是否有记录
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public int GetBug(int workParentId)
        {
            string sql = "select count(*) from Bugs where WorkParentId=@WorkParentId";
            return Convert.ToInt32(SQLHelper.GetSingleResult(sql, new SqlParameter[] { new SqlParameter("@WorkParentId", workParentId) }));
        }
        

        public BugClass GetBugInfo(int workParentId)
        {
            string sql = "select * from Bugs where WorkParentId=@WorkParentId";
            SqlDataReader objReader = SQLHelper.GetReader(sql, new SqlParameter[] { new SqlParameter("@WorkParentId", workParentId) });
            BugClass objBug = null;
            if (objReader.Read())
            {
                objBug = new BugClass()
                {
                    Id = Convert.ToInt32(objReader["Id"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    PName = objReader["PName"].ToString(),
                    MName = objReader["MName"].ToString(),
                    QuestionType = objReader["QuestionType"].ToString(),
                    QuestionDescription = objReader["QuestionDescription"].ToString(),
                    CompleteDay = Convert.ToDateTime(objReader["CompleteDay"]),
                    MoldPerson = objReader["MoldPerson"].ToString(),
                    PlanDescription = objReader["PlanDescription"].ToString(),
                    CreateTime = Convert.ToDateTime(objReader["CreateTime"])
                };
            }
            objReader.Close();
            return objBug;
        }
        /// <summary>
        /// 根据节点名称查询bug信息
        /// </summary>
        /// <param name="nodeName"></param>
        /// <returns></returns>
        public List<BugClass> GetAllBugInfo(string nodeName,string pName,string mName)
        {
            string sql = "select * from WorkflowStatuses inner join WorkflowChildStatuses on WorkflowStatuses.WorkParentId";
            sql += "=WorkflowChildStatuses.WorkParentId left join Bugs on WorkflowStatuses.WorkParentId=";
            sql += "Bugs.WorkParentId inner join UserInfoes on WorkflowStatuses.CreaterUserName = UserInfoes.UserName";
            sql += " where nodeName=@NodeName and  PName=@PName and WorkflowStatuses.StatusDefine>=-1 and WorkflowStatuses.StatusDefine<=1 and WorkflowChildStatuses.StatusNo=1";
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@NodeName",nodeName));
            param.Add(new SqlParameter("@PName",pName));
            if(mName!="全部")
            {
                sql += " and MName=@MName";
                param.Add(new SqlParameter("@MName", mName));
            }
            SqlDataReader objReader = SQLHelper.GetReader(sql,param.ToArray());
            List<BugClass> list = new List<BugClass>();
            while(objReader.Read())
            {
                list.Add(new BugClass
                {
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    PName = objReader["PName"] is DBNull ? "" : objReader["PName"].ToString(),
                    MName = objReader["MName"] is DBNull ? "" : objReader["MName"].ToString(),
                    QuestionType = objReader["QuestionType"] is DBNull ? "" : objReader["QuestionType"].ToString(),
                    QuestionDescription = objReader["QuestionDescription"] is DBNull ? "" : objReader["QuestionDescription"].ToString(),
                    MoldPerson = objReader["MoldPerson"]is DBNull ? "" :objReader["MoldPerson"].ToString(),
                    PlanDescription = objReader["PlanDescription"] is DBNull ? "" : objReader["PlanDescription"].ToString(),
                    CreateTime = objReader[41] is DBNull ? Convert.ToDateTime("0001-01-01") : Convert.ToDateTime(objReader[41]),
                    CompleteDay = objReader["CompleteDay"] is DBNull ? Convert.ToDateTime("0001-01-01") : Convert.ToDateTime(objReader["CompleteDay"]),
                    WorkParent = new WorkflowStatus 
                    {
                        Critical = objReader["Critical"].ToString(),
                        CreaterDisplayName = objReader["DisplayName"].ToString()
                    },
                    WorkChild = new WorkflowChildStatus { IsRead = Convert.ToInt32(objReader["IsRead"]) },
                });
            }
            objReader.Close();
            return list;
        }
        #endregion

        #region 更新
        public int UpdateBug(BugClass bug)
        {
            string sql = "update Bugs set PName=@PName,MName=@MName,";
            sql += "QuestionType=@QuestionType,QuestionDescription=@QuestionDescription,";
            sql += "CompleteDay=@CompleteDay,MoldPerson=@MoldPerson,PlanDescription=@PlanDescription";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@PName",bug.PName),
                new SqlParameter("@MName",bug.MName),
                new SqlParameter("@QuestionType",bug.QuestionType),
                new SqlParameter("@QuestionDescription",bug.QuestionDescription),
                new SqlParameter("@CompleteDay",bug.CompleteDay),
                new SqlParameter("@MoldPerson",bug.MoldPerson),
                new SqlParameter("@PlanDescription",bug.PlanDescription),
                new SqlParameter("@WorkParentId",bug.WorkParentId)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion
    }
}
